<?php
session_start();

class Series extends MY_Controller{
	private $status = TRUE;
	private $token = '';
	
	function __construct(){
		parent::__construct();
		$this->load->database();
		Sys::loadLanguage($this->params['lang'],'panel');
	}

	public function _remap($method, $params){
		$res = $this->checkAccessToken();
		
		$this->load->model('user_model');
		$info = $this->user_model->getInfoUser($this->params);
		$this->params['usr_info'] = $info['user'];
		
		if($res && $this->params['usr_info']['status']==1){
			$this->load->model('user_model');
			$info = $this->user_model->getInfoUser($this->params);
			$this->params['usr_info'] = $info['user'];
			
			if($this->params['usr_info']['tipo']==='admin'){
				switch ($method) {
					case 'index':
							header('Location:'.Sys::$url_base.$this->params['lang_url'].'panel');
					break;
					//	ADMIN
					case 'admin':
							$this->admin($method,$params);
					break;
					case 'admin_cs':
							$this->admin_cs($method,$params);
					break;
					
					//	SERIES
					case 'add_serie':
							$this->serie($method,$params);
					break;
					case 'save_serie':
							$this->save($method, $params);
					break;
					case 'edit_serie':
							$this->serie($method,$params);
					break;
					case 'update_serie':
							$this->update($method,$params);
					break;
					
					//	SEASONS
					case 'add_season':
							$this->season($method,$params);
					break;
					case 'save_season':
							$this->save($method, $params);
					break;
					case 'edit_season':
							$this->season($method,$params);
					break;
					case 'update_season':
							$this->update($method,$params);
					break;				
					
					//	EPISODES
					case 'add_episode':
							$this->episode($method,$params);
					break;
					case 'save_episode':
							$this->save($method, $params);
					break;
					case 'edit_episode':
							$this->episode($method,$params);
					break;
					case 'update_episode':
							$this->update($method,$params);
					break;
					
					//	TVDB
					case 'get_list_tvdb':
							$this->get_list_tvdb($method, $params);
					break;
					case 'get_info_serie':
							$this->get_info_serie();
					break;
					case 'get_info_episode':
							$this->get_info_episode();
					break;
					
					default:
						show_404();
					break;
				}
			}else header('Location:'.Sys::$url_base.$this->params['lang_url'].'panel');
		}
		else if(isset($_COOKIE["p_goto"])){ //redireccionar a alguna url despues de logearse
			$url = $_COOKIE["p_goto"];
			setcookie('p_goto', '', time()-1200, '/');
			header("Location: ".$url);
		}else{
	    	header('Location:'.Sys::$url_base.$this->params['lang_url'].'');
		}
	}

	/**
	 * 	MUESTRA UN LISTADO DE LAS SERIES PARA PODER AGERGAR O EDITAR LA INFORMACION
	 * 	DE UNA SERIE, TEMPORADA, EPISODIO
	 * 
	 * 	@param string $method 			METODO QUE LLAMA LA FUNCION
	 * 	@param array $params			PARAMETROS QUE RECIBE, PUEDEN SER SEGMENTOS DE LA URL
	 */
	public function admin($method='', $params=''){
			
		//	TITULO DE LA SECCION
		$this->params['lang_txt_seccion'] = lang('txt_seccion_series_admin');
		
		//	CARGA LOS CSS
		$this->carabiner->css('dev/libs/jquery.treeview.css');
		$this->carabiner->css('dev/style.css');
		$this->carabiner->css('dev/panel/style.css');
		$this->carabiner->css('dev/panel/series/style_admin_series.css');
		
		//	CARGA LOS JS
		$js	= array(
					array('dev/libs/jquery.js'),
					array('dev/libs/jquery.tools.min.js'),
					array('dev/libs/jquery.treeview.js'),
					array('dev/panel/general.js'),
					array('dev/panel/series/admin.js'),
				);
		$this->carabiner->js($js);
		
		//	CARGA LA LIBRERIA DE PAGINACION Y ASIGNA LA CONFIG
		$this->load->library('pagination');
		$this->params['result_items_per_page'] = 20;
		$this->params['result_page'] = 0;
		
		//	SI EXISTE EL PARAMETRO DE LA PAGINA A MOSTRAR
		if(isset($this->params['pag'])){
			if($this->params['pag'] % $this->params['result_items_per_page'] == 0)
				$this->params['result_page'] = ($this->params['pag']/$this->params['result_items_per_page']);
		}
		
		//	CARGA EL MODELO SERIES_MODEL PARA OBTENER LAS SERIES CON SUS TEMPORADAS Y CAPITULOS
		$this->load->model('series_model');
		$res_series = $this->series_model->getSeriesFull($this->params);
		
		if(is_array($res_series['series'])){
			$this->params['list'] = $res_series;
		}

		// INDICA EL MENU PADRE Y EL SUBMENU QUE SERAN SELECCIONADOS
		$this->params['selected_menu2']		= 'mnu-selected';
		$this->params['selected_submenu3']	= 'submnu-selected';
		
		$this->params['seo'] = array(
			'title' 		=> ''.Sys::$title_web,
			'description' 	=> ''
		);
		
		// $this->params['aside_extra'] = $this->load->view('interface/panel/films/aside_buttons', $this->params, true);
		$this->load->view('interface/header',$this->params);
		$this->load->view('interface/panel/content',$this->params);
		$this->load->view('interface/panel/series/admin',$this->params);
		$this->load->view('interface/panel/aside',$this->params);
		$this->load->view('interface/footer',$this->params);	
	}

	/**
	 * 	MUESTRA UN LISTADO DE LAS TEMPORADAS QUE ESTAN COMO PROXIMOS ESTRENOS PARA PODER EDITAR LA INFORMACION
	 * 
	 * 	@param string $method 			METODO QUE LLAMA LA FUNCION
	 * 	@param array $params			PARAMETROS QUE RECIBE, PUEDEN SER SEGMENTOS DE LA URL
	 */
	public function admin_cs($method='', $params=''){
		
		//	TITULO DE LA SECCION
		$this->params['lang_txt_seccion'] = lang('txt_seccion_series_admin_cs');
		
		//	CARGA LOS CSS
		$this->carabiner->css('dev/style.css');
		$this->carabiner->css('dev/panel/style.css');
		$this->carabiner->css('dev/panel/films/style_admin_films.css');
		
		//	CARGA LOS JS
		$js	= array(
					array('dev/libs/jquery.js'),
					array('dev/libs/jquery.tools.min.js'),
					array('dev/panel/general.js'),
				);
		$this->carabiner->js($js);
		
		//	CARGA EL MODELO PUBLICATION_MODEL PARA OBTENER LOS PROXIMOS ESTRENOS
		$this->load->model('publication_model');		
		$res_films_cs = $this->publication_model->getComingSoon($this->params);
		
		if(is_array($res_films_cs['series'])){
			$this->params['list'] = $res_films_cs['series'];
		}

		// INDICA EL MENU PADRE Y EL SUBMENU QUE SERAN SELECCIONADOS
		$this->params['selected_menu2']		= 'mnu-selected';
		$this->params['selected_submenu5']	= 'submnu-selected';
		
		$this->params['seo'] = array(
			'title' 		=> ''.Sys::$title_web,
			'description' 	=> ''
		);
		
		// $this->params['aside_extra'] = $this->load->view('interface/panel/films/aside_buttons', $this->params, true);
		$this->load->view('interface/header',$this->params);
		$this->load->view('interface/panel/content',$this->params);
		$this->load->view('interface/panel/series/admin_cs',$this->params);
		$this->load->view('interface/panel/aside',$this->params);
		$this->load->view('interface/footer',$this->params);
	}


	/**
	 * MUESTRA EL FORMULARIO PARA AGREGAR UNA SERIE
	 * 
	 * @param $method			Metodo que llama la funcion
	 * @param $params			Parametros del Segmento de la URL u otros
	 */
	public function serie($method = '', $params=''){
		
		//	CARGA LOS CSS
		$this->carabiner->css('dev/libs/superbox.css');
		$this->carabiner->css('dev/style.css');
		$this->carabiner->css('dev/panel/style.css');
		$this->carabiner->css('dev/panel/style_form.css');
		
		//	CARGA LOS JS
		$js	= array(
					array('dev/libs/jquery.js'),
					array('dev/libs/superbox.jquery.js'),
					array('dev/libs/jquery.tools.min.js'),
					array('dev/panel/general.js'),
					array('dev/panel/series/series.js')
				);
		$this->carabiner->js($js);
		
		//	VARIABLE QUE SIRVE PARA INDICAR SI EXISTE ALGUN ERROR DE VALIDACION DE LOS PARAMETROS
		$show_error = FALSE;
		
		//	SI SE AGREGA O GUARDA UNA SERIE
		if($method === 'add_serie' || $method === 'save_serie'){

			//	TITULO DE LA SECCION
			$this->params['lang_txt_seccion'] = lang('txt_seccion_serie_add');

			$this->params['title'] 			= '';
			$this->params['date_release']	= '';
			$this->params['sinopsis_spa']	= '';
			$this->params['sinopsis_eng']	= '';
			$this->params['on_fb']			= '';
			$this->params['coming_soon']	= '';

			// SI EXISTE ALGUN MENSAJE
			if (isset($params['msg_action'])){
				$this->params['class_msg']	= 'msg-ok';
				$this->params['msg_action']	= $params['msg_action'];
			}

			// INDICA EL MENU PADRE Y EL SUBMENU QUE SERAN SELECCIONADOS
			$this->params['selected_menu2']	= 'mnu-selected';
			$this->params['selected_submenu4']	= 'submnu-selected';

			// EN CASO DE CARGAR EL FORMULARIO SE TIENEN QUE INDICAR ESTOS PARAMETROS LOS CUALES SERAN PARTE DEL ACTION DEL FORMULARIO
			$this->params['controller'] 	= 'series/';
			$this->params['method'] 		= 'save_serie/';
			$this->params['id']				= '';

			$this->params['seo'] = array(
				'title' 		=> ''.Sys::$title_web,
				'description' 	=> ''
			);

			//	OBTIENE LAS CATEGORIAS ALMACENADOS
			$this->db->select('id_categoria, nombre')->from('categorias')->order_by('nombre', 'ASC');
			$servers = $this->db->get();
			$this->params['categories'] = $servers->result_array();
		}
		//	SI SE EDITA O ACTUALIZA UNA SERIE
		else if($method === 'edit_serie' || $method === 'update_serie'){
			
			// ASIGNA EL ID DE LA SERIE A EDITAR
			$this->params['serie'] = $params[0];
			
			// OBTIENE EL TOTAL DE SERIES QUE EXISTE PARA EL ID DE LA SERIE QUE SE EDITARA
			$this->db->select('id_serie')->from('series')->where('id_serie',$this->params['serie']);
			$total = $this->db->count_all_results();
			
			// SI EXISTE LA SERIE QUE SE EDITARA ENTONCES ENTRA
			if($total==1){
				
				// TITULO DE LA SECCION
				$this->params['lang_txt_seccion'] = lang('txt_seccion_serie_edit');
				
				$this->params['title'] 			= '';
				$this->params['date_release']	= '';
				$this->params['sinopsis_spa']	= '';
				$this->params['sinopsis_eng']	= '';
				$this->params['on_fb']			= '';
				$this->params['coming_soon']	= '';
				
				// SI EXISTE ALGUN MENSAJE
				if (isset($params['msg_action'])){
					$this->params['class_msg']	= 'msg-ok';
					$this->params['msg_action']	= $params['msg_action'];
				}
	
				// INDICA EL MENU PADRE Y EL SUBMENU QUE SERAN SELECCIONADOS
				$this->params['selected_menu2']		= 'mnu-selected';
				$this->params['selected_submenu3']	= 'submnu-selected';
	
				// EN CASO DE CARGAR EL FORMULARIO SE TIENEN QUE INDICAR ESTOS PARAMETROS LOS CUALES SERAN PARTE DEL ACTION DEL FORMULARIO
				$this->params['controller'] 	= 'series/';
				$this->params['method'] 		= 'update_serie/';
				$this->params['id']				= $this->params['serie'];
	
				$this->params['seo'] = array(
					'title' 		=> ''.Sys::$title_web,
					'description' 	=> ''
				);
	
				//	OBTIENE LAS CATEGORIAS ALMACENADOS
				$this->db->select('id_categoria, nombre')->from('categorias')->order_by('nombre', 'ASC');
				$servers = $this->db->get();
				$this->params['categories'] = $servers->result_array();
			}
			else{
				$show_error = TRUE;
				$this->params['msg'] = lang('txt_serie_id_update_error');
				$this->params['seo'] = array(
					'title' 		=> ''.Sys::$title_web,
					'description' 	=> $this->params['msg']
				);
			}		
		}

		if($show_error){
			$this->load->view("interface/header", $this->params);
			$this->load->view("interface/errors/page_error", $this->params);
			$this->load->view("interface/panel/aside", $this->params);
			$this->load->view("interface/footer", $this->params);
		}
		else{
			$this->params['method_iframe'] = 'get_list_tvdb/serie';
			// $this->params['show_on_fb'] 		= TRUE;
			// $this->params['show_coming_soon'] 	= TRUE;
			
			$this->params['aside_extra'] = $this->load->view('interface/panel/films/aside_buttons', $this->params, true);
			$this->load->view('interface/header',$this->params);
			$this->load->view('interface/panel/content',$this->params);
			$this->load->view('interface/panel/series/form_series',$this->params);
			$this->load->view('interface/panel/aside',$this->params);
			$this->load->view('interface/footer',$this->params);
		}
	}
	
	/**
	 * MUESTRA EL FORMULARIO PARA AGREGAR UNA TEMPORADA
	 * 
	 * @param $method			Metodo que llama la funcion
	 * @param $params			Parametros del Segmento de la URL u otros
	 */
	public function season($method = '', $params=''){
		
		// CARGA LOS CSS
		$this->carabiner->css('dev/libs/superbox.css');
		$this->carabiner->css('dev/style.css');
		$this->carabiner->css('dev/panel/style.css');
		$this->carabiner->css('dev/panel/style_form.css');
		
		// CARGA LOS JS
		$js	= array(
					array('dev/libs/jquery.js'),
					array('dev/libs/superbox.jquery.js'),
					array('dev/libs/jquery.tools.min.js'),
					array('dev/panel/general.js'),
					array('dev/panel/series/series.js')
				);
		$this->carabiner->js($js);
		
		//	VARIABLE QUE SIRVE PARA INDICAR SI EXISTE ALGUN ERROR DE VALIDACION DE LOS PARAMETROS
		$show_error = FALSE;
		
		//	SI SE AGREGA O GUARDA UNA TEMPORADA
		if($method === 'add_season' || $method === 'save_season'){
			
			// ASIGAN EL ID DE LA SERIE
			$this->params['serie'] = $params[0];
			
			// OBTIENE EL TOTAL DE SERIES QUE EXISTE PARA EL ID DE LA SERIE
			$this->db->select('id_serie')->from('series')->where('id_serie',$this->params['serie']);
			$total = $this->db->count_all_results();
				
			if($total==1){
					
				//	TITULO DE LA SECCION
				$this->params['lang_txt_seccion'] = lang('txt_seccion_serie_season_add');

				$this->params['no_season']		= '';
				$this->params['date_release']	= '';
				$this->params['poster']			= '';
				$this->params['coming_soon']	= '';
				$this->params['trailers']		= '';
				$this->params['poster'] 		= '';
	
				// SI EXISTE ALGUN MENSAJE
				if (isset($params['msg_action'])){
					$this->params['class_msg']	= 'msg-ok';
					$this->params['msg_action']	= $params['msg_action'];
				}
	
				// INDICA EL MENU PADRE Y EL SUBMENU QUE SERAN SELECCIONADOS
				$this->params['selected_menu2']	= 'mnu-selected';
				$this->params['selected_submenu3']	= 'submnu-selected';
	
				// EN CASO DE CARGAR EL FORMULARIO SE TIENEN QUE INDICAR ESTOS PARAMETROS LOS CUALES SERAN PARTE DEL ACTION DEL FORMULARIO
				$this->params['controller'] 	= 'series/';
				$this->params['method'] 		= 'save_season/';
				$this->params['id']				= $this->params['serie'];
	
				$this->params['seo'] = array(
					'title' 		=> ''.Sys::$title_web,
					'description' 	=> ''
				);
				
			}else{
				// MUESTA MENSAJE DE ERROR EN CASO DE QUE NO EXISTA LA SERIE A LA QUE SE AGREGARA LA TEMPORADA
				$show_error = TRUE;
				$this->params['msg'] = lang('txt_serie_season_id_serie_error');
				$this->params['seo'] = array(
					'title' 		=> ''.Sys::$title_web,
					'description' 	=> $this->params['msg']
				);
			}	
		}
		// SI SE EDITA O ACTUALIZA UN TEMPORADA
		else if($method === 'edit_season' || $method === 'update_season'){
			
			// TITULO DE LA SECCION
			$this->params['lang_txt_seccion'] = lang('txt_seccion_serie_season_edit');
			
			// ASIGNA EL ID DE LA TEMPORADA
			$this->params['season'] = $params[0];
			$response_errors = $this->validate('get_season');
			
			if($this->status){
				
				// CARGA EL MODEL SERIES_MODEL PARA OBTENER LA INFORMACION DE UNA TEMPORADA
				$this->load->model('series_model');
				$season_info = $this->series_model->getSeason($this->params);
				
				// SI EXISTE INFORMACION DE LA TEMPORADA
				if(is_array($season_info)){
					
					// OBTIENE EL ID Y TITULO DE LAS SERIES EXISTENTES
					$res = $this->db->select('id_serie as id, titulo')->from('series')->get();
					$this->params['list'] = $res->result_array();
					
					$this->params['id_serie']		= $season_info['id_serie'];
					$this->params['no_season'] 		= $season_info['no_season'];
					$this->params['poster'] 		= '';//$season_info['url_poster_big']
					$this->params['date_release']	= $season_info['date_release'];
					$this->params['coming_soon']	= (($season_info['coming_soon'])==1) ? 'checked' : '' ;
					$this->params['trailers']		= $season_info['trailer'];
					
					// SI EXISTE ALGUN MENSAJE
					if (isset($params['msg_action'])){
						$this->params['class_msg']	= 'msg-ok';
						$this->params['msg_action']	= $params['msg_action'];
					}
		
					// INDICA EL MENU PADRE Y EL SUBMENU QUE SERAN SELECCIONADOS
					$this->params['selected_menu2']		= 'mnu-selected';
					$this->params['selected_submenu3']	= 'submnu-selected';
		
					// EN CASO DE CARGAR EL FORMULARIO SE TIENEN QUE INDICAR ESTOS PARAMETROS LOS CUALES SERAN PARTE DEL ACTION DEL FORMULARIO
					$this->params['controller'] 	= 'series/';
					$this->params['method'] 		= 'update_season/';
					$this->params['id']				= $this->params['season'];
		
					$this->params['seo'] = array(
						'title' 		=> ''.Sys::$title_web,
						'description' 	=> ''
					);
				}
				else{
					// SI NO EXISTE LA TEMPORADA DE LA QUE SE QUIERE OBTENER LA INFORMACION
					$show_error = TRUE;
					$this->params['msg'] = lang('txt_season_id_update_error');
				}
			}
			else{
				// SI EXISTEN UN ERROR DE VALIDACION DE LOS PARAMETROS
				$show_error = TRUE;
				$this->params['msg'] = implode(',', $response_errors);
			}
		}

		// SI EXISTE ALGUN ERROR LOS MUESTRA
		if($show_error){
			$this->params['seo'] = array(
						'title' 		=> ''.Sys::$title_web,
						'description' 	=> $this->params['msg']
					);
			
			$this->load->view("interface/header", $this->params);
			$this->load->view("interface/errors/page_error", $this->params);
			$this->load->view("interface/panel/aside", $this->params);
			$this->load->view("interface/footer", $this->params);
		}
		else{
			$this->params['method_iframe'] = 'get_list_tvdb/season';
			// $this->params['show_on_fb'] 		= TRUE;
			$this->params['show_coming_soon'] 	= TRUE;
			
			$this->params['aside_extra'] = $this->load->view('interface/panel/films/aside_buttons', $this->params, true);
			$this->load->view('interface/header',$this->params);
			$this->load->view('interface/panel/content',$this->params);
			$this->load->view('interface/panel/series/form_season',$this->params);
			$this->load->view('interface/panel/aside',$this->params);
			$this->load->view('interface/footer',$this->params);
		}
	}
	
	/**
	 * MUESTRA EL FORMULARIO PARA AGREGAR UN EPISODIO
	 * 
	 * @param $method			Metodo que llama la funcion
	 * @param $params			Parametros del Segmento de la URL u otros
	 */
	public function episode($method = '', $params=''){
			
		// CARGA LOS CSS
		$this->carabiner->css('dev/libs/superbox.css');
		$this->carabiner->css('dev/style.css');
		$this->carabiner->css('dev/panel/style.css');
		$this->carabiner->css('dev/panel/style_form.css');
		
		// CARGA LOS JS
		$js	= array(
					array('dev/libs/jquery.js'),
					array('dev/libs/superbox.jquery.js'),
					array('dev/libs/jquery.tools.min.js'),
					array('dev/panel/general.js'),
					array('dev/panel/series/series.js')
				);
		$this->carabiner->js($js);
		
		//	VARIABLE QUE SIRVE PARA INDICAR SI EXISTE ALGUN ERROR DE VALIDACION DE LOS PARAMETROS
		$show_error = FALSE;
		
		// SI SE AGREGA O GUARDA UN EPISODIO
		if($method === 'add_episode' || $method === 'save_episode'){
			
			// ASIGNA EL ID DE LA TEMPORADA
			$this->params['season'] = $params[0];
			
			// OBTIENE EL TOTAL DE TEMPORADAS QUE EXISTE CON EL ID DE LA TEMPORADA
			$this->db->select('id_temporada')->from('series_temporadas')->where('id_temporada',$this->params['season']);
			$total = $this->db->count_all_results();
			
			// SI EXISTE LA TEMPORADA
			if($total==1){

				// TITULO DE LA SECCION
				$this->params['lang_txt_seccion'] = lang('txt_seccion_serie_episode_add');

				$this->params['no_episode']		= '';
				$this->params['title']			= '';
				$this->params['format_episode']	= '';
				$this->params['quality']		= '';
				$this->params['duration']		= '';
				$this->params['size']			= '';
				$this->params['languaje']		= '';
				$this->params['subtitles']		= '';
				$this->params['sinopsis_spa']	= '';
				$this->params['sinopsis_eng']	= '';
				$this->params['screenshots']	= '';
				$this->params['on_fb']			= '';
	
				// SI EXISTE ALGUN MENSAJE
				if (isset($params['msg_action'])){
					$this->params['class_msg']	= 'msg-ok';
					$this->params['msg_action']	= $params['msg_action'];
				}
	
				// INDICA EL MENU PADRE Y EL SUBMENU QUE SERAN SELECCIONADOS
				$this->params['selected_menu2']	= 'mnu-selected';
				$this->params['selected_submenu3']	= 'submnu-selected';
	
				// EN CASO DE CARGAR EL FORMULARIO SE TIENEN QUE INDICAR ESTOS PARAMETROS LOS CUALES SERAN PARTE DEL ACTION DEL FORMULARIO
				$this->params['controller'] 	= 'series/';
				$this->params['method'] 		= 'save_episode/';
				$this->params['id']				= $this->params['season'];
	
				$this->params['seo'] = array(
					'title' 		=> ''.Sys::$title_web,
					'description' 	=> ''
				);
				
				//	OBTIENE LOS SERVIDORES ALMACENADOS
				$this->db->select('id_servidor, nombre')->from('servidores');
				$servers = $this->db->get();
				$this->params['servers'] = $servers->result_array();
				
			}else{
				// SI NO EXISTE LA TEMPORADA A LA QUE SE AGREGARA EL EPISODIO
				$show_error = TRUE;
				$this->params['msg'] = lang('txt_season_episode_id_serie_error');
				$this->params['seo'] = array(
					'title' 		=> ''.Sys::$title_web,
					'description' 	=> $this->params['msg']
				);
			}	
		}
		// SI SE EDITA O ACTUALIZA UN EPISODIO
		else if($method === 'edit_episode' || $method === 'update_episode'){
			
			// TITULO DE LA SECCION
			$this->params['lang_txt_seccion'] = lang('txt_seccion_serie_episode_edit');
			// ASIGNA EL ID DEL EPISODIO
			$this->params['episode'] = $params[0];
			
			$response_errors = $this->validate('get_episode');
			if($this->status){
				
				// CARGA EL MODEL SERIES_MODEL PARA OBTENER LA INFORMACION DEL EPISODIO
				$this->load->model('series_model');
				$episode_info = $this->series_model->getEpisode($this->params);
				
				// SI EL EPISODIO Y SU INFORMACION
				if(is_array($episode_info)){
					
					// OBTIENE LAS TEMPORADAS EXISTENTES PARA LA SERIE QUE SE QUIERE AGEGAR EL EPISODIO
					$res = $this->db->select('id_temporada as id, no_temporada as titulo ')->from('series_temporadas')->where('id_serie',$episode_info['id_serie'])->get();
					$this->params['list'] = $res->result_array();
					
					$this->params['title_serie']	= $episode_info['title_serie'];
					$this->params['id_season']		= $episode_info['id_season'];
					$this->params['no_episode']		= $episode_info['no_episode'];
					$this->params['title']			= $episode_info['title_episode'];
					$this->params['format_episode']	= $episode_info['format_episode'];
					$this->params['quality']		= $episode_info['quality'];
					$this->params['duration']		= $episode_info['duration'];
					$this->params['size']			= $episode_info['size'];
					$this->params['languaje']		= $episode_info['language'];
					$this->params['subtitles']		= $episode_info['subtitle'];
					$this->params['sinopsis_spa']	= $episode_info['synopsis_spa'];
					$this->params['sinopsis_eng']	= $episode_info['synopsis_eng'];
					$this->params['screenshots']	= '';//$episode_info['no_episode']
					$this->params['on_fb']			= (is_null($episode_info['id_facebook'])) ? '' : 'checked' ;					
					
					$this->params['servers_episode']= isset($episode_info['sources']) ? $episode_info['sources'] : null;
					
					// SI EXISTE ALGUN MENSAJE
					if (isset($params['msg_action'])){
						$this->params['class_msg']	= 'msg-ok';
						$this->params['msg_action']	= $params['msg_action'];
					}
		
					// INDICA EL MENU PADRE Y EL SUBMENU QUE SERAN SELECCIONADOS
					$this->params['selected_menu2']		= 'mnu-selected';
					$this->params['selected_submenu3']	= 'submnu-selected';
		
					// EN CASO DE CARGAR EL FORMULARIO SE TIENEN QUE INDICAR ESTOS PARAMETROS LOS CUALES SERAN PARTE DEL ACTION DEL FORMULARIO
					$this->params['controller'] 	= 'series/';
					$this->params['method'] 		= 'update_episode/';
					$this->params['id']				= $this->params['episode'];
		
					$this->params['seo'] = array(
						'title' 		=> ''.Sys::$title_web,
						'description' 	=> ''
					);
					
					//	OBTIENE LOS SERVIDORES ALMACENADOS
					$this->db->select('id_servidor, nombre')->from('servidores');
					$servers = $this->db->get();
					$this->params['servers'] = $servers->result_array();
					
				}
				else{
					// SI NO EXISTE EL EPISODIO QUE SE QUIERE EDITAR
					$show_error = TRUE;
					$this->params['msg'] = lang('txt_season_id_update_error');
				}
			}
			else{
				// SI EXISTE ERRORES DE VALIDACION DE LOS PARAMETROS
				$show_error = TRUE;
				$this->params['msg'] = implode(',', $response_errors);
			}
		}

		// SI EXISTE ALGUN ERROR DE VALIDACION
		if($show_error){
			$this->params['seo'] = array(
						'title' 		=> ''.Sys::$title_web,
						'description' 	=> $this->params['msg']
					);
			
			$this->load->view("interface/header", $this->params);
			$this->load->view("interface/errors/page_error", $this->params);
			$this->load->view("interface/panel/aside", $this->params);
			$this->load->view("interface/footer", $this->params);
		}
		else{
			$this->params['method_iframe'] = 'get_list_tvdb/episode';
			$this->params['show_on_fb'] 		= TRUE;
			// $this->params['show_coming_soon'] 	= TRUE;
			
			$this->params['aside_extra'] = $this->load->view('interface/panel/films/aside_buttons', $this->params, true);
			$this->load->view('interface/header',$this->params);
			$this->load->view('interface/panel/content',$this->params);
			$this->load->view('interface/panel/series/form_episode',$this->params);
			$this->load->view('interface/panel/aside',$this->params);
			$this->load->view('interface/footer',$this->params);
		}
	}
	
	/**
	 *	GUARDA LA INFORMACION ENVIADA DEL FORMULARIO YA SEA SERIE, TEMPORADA O EPISODIO
	 * 
	 * 	@param string $method			METODO QUE LLAMA LA FUNCION
	 * @param array $params				Parametros del Segmento de la URL u otros
	 */
	public function save($method, $params=''){
		
		// SI SE GUARDA UNA SERIE
		if($method === 'save_serie')
			$method_validate = '';
		// SI SE GUARDARA UNA TEMPORADA
		else if($method === 'save_season'){
			$method_validate = '_season';
			$this->params['serie'] = $params[0];
			$this->params['coming_soon'] = (isset($this->params['coming_soon']) ? $this->params['coming_soon'] : '0');
		}
		// SI SE GUARDARA UN EPISODIO
		else if($method === 'save_episode'){
			$method_validate = '_episode';
			$this->params['season'] = $params[0];
		}
		
		// VALIDA LOS PARAMETROS DEPENDIENDO QUE SE GUARDARA SI UNA SERIE, TEMPORADA O EPISODIO
		$response_errors = $this->validate('add'.$method_validate);
		
		// INDICA SI EXISTE ALGUN ERROR
		$show_error = FALSE;
		
		// SI NO EXISTE ERRORES EN LOS PARAMETROS VALIDADOS
		if($this->status == TRUE){
			
			// CARGA EL MODEL SERIE_MODEL PARA GUARDAR UNA SERIE, TEMPORADA O EPISODIO
			$this->load->model('series_model');
			
			if($method === 'save_serie'){
				$serie = $this->series_model->addSerie($this->params);
				if(is_array($serie))
					$this->serie('save_serie',array('msg_action'=>lang('txt_serie_save_ok')));
			}
			else if($method === 'save_season'){
				$season = $this->series_model->addSeason($this->params);
				if(is_array($season))
					$this->season('save_season',array( $this->params['serie'] , 'msg_action'=>lang('txt_season_save_ok')));
				else if($season==2){
					$this->params['msg'] = lang('txt_poster_error');
					$this->params['seo'] = array(
						'title' 		=> ''.Sys::$title_web,
						'description' 	=> ''
					);
					$show_error = TRUE;
				}
				else if($season==1){
					$this->params['msg'] = lang('txt_serie_season_id_serie_error');
					$this->params['seo'] = array(
						'title' 		=> ''.Sys::$title_web,
						'description' 	=> ''
					);
					$show_error = TRUE;
				}
			}
			else if($method === 'save_episode'){
				$episode = $this->series_model->addEpisode($this->params);
				if(is_array($episode))
					$this->episode('save_episode',array( $this->params['season'] , 'msg_action'=>lang('txt_episode_save_ok')));
				else{
					$this->params['msg'] = lang('txt_season_episode_id_serie_error');
					$this->params['seo'] = array(
						'title' 		=> ''.Sys::$title_web,
						'description' 	=> ''
					);
					$show_error = TRUE;
				}	
			}
				
		}else{
			// SI EXISTEN ERRORES DE VALIDACION DE LOS PARAMETROS
			$show_error = TRUE;
			$this->params['msg'] = implode(',', $response_errors);
			$this->params['seo'] = array(
				'title' 		=> ''.Sys::$title_web,
				'description' 	=> $this->params['msg']
			);
		}
		
		// SI EXISTE UN ERROR LOS MUESTRA
		if($show_error){
			$this->carabiner->css('dev/style.css');
			$this->carabiner->css('dev/panel/style.css');
			
			$js	= array(
						array('dev/libs/jquery.js'),
						array('dev/libs/jquery.tools.min.js'),
						array('dev/panel/general.js')
					);
					
			$this->carabiner->js($js);
					
			$this->load->view("interface/header", $this->params);
			$this->load->view("interface/errors/page_error", $this->params);
			$this->load->view("interface/panel/aside", $this->params);
			$this->load->view("interface/footer", $this->params);
		}
	}

	/**
	 *	ACTUALIZA LA INFORMACION ENVIADA DEL FORMULARIO YA SEA SERIE, TEMPORADA O EPISODIO
	 * 
	 * 	@param string $method			METODO QUE LLAMA LA FUNCION
	 * 	@param array $params			Parametros del Segmento de la URL u otros
	 */
	public function update($method='', $params=''){
		
		// SI SE ACTUALIZARA UNA SERIE
		if($method === 'update_serie'){
			$method_validate = '';
			$this->params['serie'] = $params[0];
		}
		// SI SE ACTUALIZARA UNA TEMPORADA
		else if($method === 'update_season'){
			$method_validate = '_season';
			$this->params['season'] = $params[0];
			$this->params['coming_soon'] = (isset($this->params['coming_soon']) ? $this->params['coming_soon'] : '0');
		}
		// SI SE ACTUALIZARA UN EPISODIO
		else if($method === 'update_episode'){
			$method_validate = '_episode';
			$this->params['episode'] = $params[0];
		}
		
		// VALIDA LOS PARAMETROS PASADO YA SEA SERIE, TEMPORADA O EPISODIO
		$response_errors = $this->validate('update'.$method_validate);
		
		// SI NO EXISTEN ERRORES DE VALIDACION
		if($this->status == TRUE){
			
			// CARGA EL MODELO SERIES_MODEL PARA ACTUALIZAR LA SERIE, TEMPORADA O EPISODIO
			$this->load->model('series_model');
			
			if($method === 'update_serie'){
				$serie = $this->series_model->updateSerie($this->params);
				if(is_array($serie))
					$this->serie('update_serie',array($this->params['serie'], 'msg_action'=>lang('txt_serie_update_ok')));
			}
			else if($method === 'update_season'){
				$season = $this->series_model->updateSeason($this->params);
				if(is_array($season))
					$this->season('update_season',array($this->params['season'],'msg_action'=>lang('txt_season_update_ok')));
			}
			else if($method === 'update_episode'){
				$episode = $this->series_model->updateEpisode($this->params);
				if(is_array($episode))
					$this->episode('update_episode',array($this->params['episode'],'msg_action'=>lang('txt_episode_update_ok')));
			}	

		}else{
			
			// SI EXISTE ERRORES DE VALIDACION DE PARAMETROS
			$this->carabiner->css('dev/style.css');
			$this->carabiner->css('dev/panel/style.css');
			
			$js	= array(
						array('dev/libs/jquery.js'),
						array('dev/libs/jquery.tools.min.js'),
						array('dev/panel/general.js')
					);
			
			$this->carabiner->js($js);
			$this->params['msg'] = implode(',', $response_errors);
			$this->params['seo'] = array(
				'title' 		=> ''.Sys::$title_web,
				'description' 	=> $this->params['msg']
			);
			
			$this->load->view("interface/header", $this->params);
			$this->load->view("interface/errors/page_error", $this->params);
			$this->load->view("interface/panel/aside", $this->params);
			$this->load->view("interface/footer", $this->params);
		}
	}

	/**
	 *	OBTIENE UN LISTADO DE SERIES DE TVDB 
	 * 
	 * 	@param string $method			METODO QUE LLAMA LA FUNCION
	 * 	@param array $params			Parametros del Segmento de la URL u otros
	 */
	public function get_list_tvdb($method,$params){

		// CARGA LOS CSSS
		$this->carabiner->css('dev/style.css');
		$this->carabiner->css('dev/panel/style.css');
		$this->carabiner->css('dev/panel/style_form.css');
		
		// SI ES UNA SERIE CARGA EL JS INDICADO
		if($params[0]==='serie'){
			$js_iframe = array('dev/panel/series/iframe_search_series.js');
			$this->params['method_action'] = 'serie';
		}
		// SI ES UNA TEMPORADA CARGA EL JS INDICADO
		else if($params[0]==='season'){
			$js_iframe = array('dev/panel/series/iframe_search_season.js');
			$this->params['method_action'] = 'season';
		}
		// SI ES UN EPISODIO CARGA EL JS INDICADO
		else if($params[0]==='episode'){
			$js_iframe = array('dev/panel/series/iframe_search_episode.js');
			$this->params['method_action'] = 'episode';
		}
		
		$js	= array(
					array('dev/libs/jquery.js'),
					$js_iframe
				);
		$this->carabiner->js($js);
		
		$this->params['seo'] = array(
						'title' 		=> ''.Sys::$title_web,
						'description' 	=> ''
					);
		
		// SI EXISTE EL TEXTO DE BUSQUEDA
		if(isset($this->params['title'])){
			
			// VALIDA QUE LOS PARAMETROS PARA LA BUSQUEDA SEAN CORRECTOS
			$response_errors = $this->validate('search_tvdb_search');
			
			if($this->status){
				
				// CARGA EL MODELO SERIES_MODEL PARA REALIZARA LA BUSQUEDA
				$this->load->model('series_model');
				$list = $this->series_model->tvdbSearch($this->params);
				
				// SI EXISTEN RESULTADOS DE LA BUSQUEDASS
				if(isset($list['serie_list'])){
					$this->params['list'] = $list['serie_list'];
				}
				// SI EXISTE ALGUN ERROR DE BUSQUEDA LOS CARGA
				else{
					$this->params['msg'] = $list['list'];
					$this->params['error_params'] = $this->load->view("interface/errors/page_error", $this->params, TRUE);
				}
			}
			else{
				// SI EXISTE ERROR EN LOS PARAMETROS DE VALIDACION PARA LA BUSQUEDA
				$this->params['msg'] = implode(',', $response_errors);				
				$this->params['error_params'] = $this->load->view("interface/errors/page_error", $this->params, TRUE);
			}
		}		
		
		$this->load->view('interface/panel/series/frame_search_get_series',$this->params);
	}

	/**
	 *	METODO LLAMADO POR AJAX
	 * 
	 * 	OBTIENE LA INFORMACION DE UNA  SERIES DE TVDB
	 */
	public function get_info_serie(){
		
		// VALIDA QUE LOS PARAMETROS PARA OBTENER LA INFORMACION SEAN CORRECTOS
		$response_errors = $this->validate('tvdb_serie');
		
		if($this->status){
			
			// CARGA EL MODEL SERIES_MODEL PARA OBTENER LA INFORMACION DE LA SERIE
			$this->load->model('series_model');
			$info_serie = $this->series_model->tvdbSerie($this->params);
			
			// SI EXISTE INFORMACION DE LA SERIE REALIZA EL ECHO
			if(isset($info_serie['serie']))
				echo json_encode($info_serie['serie']);
			// SI HUBO ALGUN ERROR AL TRATAR DE RECUPERAR LA INFORMACION DE LA SERIE
			else
				echo json_encode(array('error'=>'Vuelva a intentarlo'));
		}
		else{
			// SI LOS PARAMETROS DE VALIDACION PARA OBTENER LA INFORMACION DE LA SERIE ESTAN MAL
			$val = implode(',', $response_errors);
			echo json_encode(array('error' => $val));
		}
	}

	/**
	 *	METODO LLAMA POR AJAX
	 * 
	 * 	OBTIENE LA INFORMACION DE UN EPISODIO DE TVDB
	 * 
	 */
	public function get_info_episode(){
		
		// VALIDA LOS PARAMETROS NECESARIOS PARA OBTENER LA INFORMACION DEL EPISODIO	
		$response_errors = $this->validate('tvdb_episode');
		
		if($this->status){
			
			// CARGA EL MODEL SERIES_MODEL PARA OBTENER LA INFORMACION DEL EPISODIO
			$this->load->model('series_model');
			$info_episode = $this->series_model->tvdbEpisode($this->params);
			
			// SI EXISTE INFORMACION DEL EPISODIO REALIZA EL ECHO
			if(isset($info_episode['episode']))
				echo json_encode($info_episode['episode']);
			// SI NO EXISTE INFORMACION PARA EL EPISODIO 
			else
				echo json_encode(array('error'=>'Nada encontrado. Vuelva a intentarlo'));
		}
		else{
			// SI EXISTEN ALGUN ERROR EN LOS PARAMETRO DE VALIDACION PARA OBTENER LA INFORMACION DEL EPISODIO
			$val = implode(',', $response_errors);
			echo json_encode(array('error' => $val));
		}
	}
	
	/**
     * Permite incorporar las validaciones bases que apliquen para todos los servicios.
     * Validaciones mas espesificas tendran que ser en cada servicio en particular.
     * @author gama
     */
    protected function validate($val_method){
    	if(method_exists($this, 'validate_'.$val_method)){
    		$this->load->library('MY_Form_validation');
    		return $this->{'validate_'.$val_method}();
    	}
    }
	
	
	private function validate_add(){
    	$validate = new MY_Form_validation();
    	$validate->setArrayValidate($this->params);
    	
    	$validate->set_rules(array(
    			array('field' => 'title', 
                     'label' => 'title', 
                     'rules' => 'required|max_length[100]'),
				array('field' => 'date_release', 
                     'label' => 'date_release', 
                     'rules' => 'required'),
				array('field' => 'cast[]', 
                     'label' => 'cast[]', 
                     'rules' => 'is_natural_no_zero'),
				array('field' => 'categories[]', 
                     'label' => 'categories[]', 
                     'rules' => 'is_natural_no_zero')
    	));
		$this->status = $validate->run();
		
		$fecha = new Fecha();
		if(isset($this->params['date_release'])){
			if(!$fecha->is_date($this->params['date_release'])){
				$this->status = FALSE;
				$validate->_error_array['date_release'] = lang('txt_invalid_date').' (date_release)';
			}
		}
    	return $validate->_error_array;
    }
	
	private function validate_update(){
    	$validate = new MY_Form_validation();
    	$validate->setArrayValidate($this->params);
    	
    	$validate->set_rules(array(
    			array('field' => 'serie', 
                     'label' => 'serie', 
                     'rules' => 'required|is_natural_no_zero'),
    			array('field' => 'title', 
                     'label' => 'title', 
                     'rules' => 'required|max_length[100]'),
				array('field' => 'date_release', 
                     'label' => 'date_release', 
                     'rules' => 'required'),
				array('field' => 'cast[]', 
                     'label' => 'cast[]', 
                     'rules' => 'is_natural_no_zero'),
				array('field' => 'categories[]', 
                     'label' => 'categories[]', 
                     'rules' => 'is_natural_no_zero')
    	));
		$this->status = $validate->run();
		
		$fecha = new Fecha();
		if(isset($this->params['date_release'])){
			if(!$fecha->is_date($this->params['date_release'])){
				$this->status = FALSE;
				$validate->_error_array['date_release'] = lang('txt_invalid_date').' (date_release)';
			}
		}
		
    	return $validate->_error_array;
    }
	
	private function validate_add_season(){
    	$validate = new MY_Form_validation();
    	$validate->setArrayValidate($this->params);
    	
		$valida_poster = array();
		if(!isset($_FILES['poster']['name']) && !isset($this->params['poster'])){
			$valida_poster = array(
				'field' => 'poster', 
				'label' => 'poster', 
				'rules' => 'required');
		}
    	$validate->set_rules(array(
    			array('field' => 'serie', 
                     'label' => 'serie', 
                     'rules' => 'required|is_natural_no_zero'),
    			array('field' => 'no_season', 
                     'label' => 'no_season', 
                     'rules' => 'required|is_natural'),
				array('field' => 'date_release', 
                     'label' => 'date_release', 
                     'rules' => 'required'),
                $valida_poster,
				array('field' => 'trailers', 
                     'label' => 'trailers', 
                     'rules' => 'max_length[30]'),
				array('field' => 'coming_soon', 
                     'label' => 'coming_soon', 
                     'rules' => 'expression:/^(0|1)$/')
    	));
		$this->status = $validate->run();
		
		$fecha = new Fecha();
		if(isset($this->params['date_release'])){
			if(!$fecha->is_date($this->params['date_release'])){
				$this->status = FALSE;
				$validate->_error_array['date_release'] = lang('txt_invalid_date').' (date_release)';
			}
		}
		
		if(isset($this->params['poster'])){
			if(Sys::urlValid($this->params['poster']) == FALSE){
				$this->status = FALSE;
				$validate->_error_array['poster'] = lang('txt_invalid_url', 'poster');
			}
		}
		
    	return $validate->_error_array;
    }
	
	private function validate_get_season(){
    	$validate = new MY_Form_validation();
    	$validate->setArrayValidate($this->params);
    	
    	$validate->set_rules(array(
    			array('field' => 'season', 
                     'label' => 'season', 
                     'rules' => 'required|is_natural_no_zero')
    	));
		$this->status = $validate->run();
		return $validate->_error_array;
    }

	private function validate_update_season(){
    	$validate = new MY_Form_validation();
    	$validate->setArrayValidate($this->params);
    	
    	$validate->set_rules(array(
    			array('field' => 'season', 
                     'label' => 'season', 
                     'rules' => 'required|is_natural_no_zero'),
    			array('field' => 'serie', 
                     'label' => 'serie', 
                     'rules' => 'required|is_natural_no_zero'),
    			array('field' => 'no_season', 
                     'label' => 'no_season', 
                     'rules' => 'is_natural'),
				array('field' => 'trailers', 
                     'label' => 'trailers', 
                     'rules' => 'alpha_numeric'),
				array('field' => 'coming_soon', 
                     'label' => 'coming_soon', 
                     'rules' => 'expression:/^(0|1)$/')
    	));
		$this->status = $validate->run();
		
		$fecha = new Fecha();
		if(isset($this->params['date_release']) && !empty($this->params['date_release'])){
			if(!$fecha->is_date($this->params['date_release'])){
				$this->status = FALSE;
				$validate->_error_array['date_release'] = lang('txt_invalid_date').' (date_release)';
			}
		}
		
		if(isset($this->params['poster']) && !empty($this->params['poster'])){
			if(Sys::urlValid($this->params['poster']) == FALSE){
				$this->status = FALSE;
				$validate->_error_array['poster'] = lang('txt_invalid_url', 'poster');
			}
		}
		
    	return $validate->_error_array;
    }
	
	private function validate_add_episode(){
    	$validate = new MY_Form_validation();
    	$validate->setArrayValidate($this->params);
    	
    	$validate->set_rules(array(
    			array('field' => 'season', 
                     'label' => 'season', 
                     'rules' => 'required|is_natural_no_zero'),
    			array('field' => 'no_episode', 
                     'label' => 'no_episode', 
                     'rules' => 'required|is_natural'),
				array('field' => 'title', 
                     'label' => 'title', 
                     'rules' => 'required|max_length[100]'),
                     
				array('field' => 'format_episode', 
                     'label' => 'format_episode', 
                     'rules' => 'max_length[10]'),
				array('field' => 'quality', 
                     'label' => 'quality', 
                     'rules' => 'max_length[20]'),
				array('field' => 'duration', 
                     'label' => 'duration', 
                     'rules' => 'max_length[30]'),
				array('field' => 'size', 
                     'label' => 'size', 
                     'rules' => 'max_length[20]'),
				array('field' => 'languaje', 
                     'label' => 'languaje', 
                     'rules' => 'max_length[20]'),
				array('field' => 'subtitles', 
                     'label' => 'subtitles', 
                     'rules' => 'max_length[20]'),
				array('field' => 'sinopsis_spa', 
                     'label' => 'sinopsis_spa', 
                     'rules' => 'max_length[10000]'),
				array('field' => 'sinopsis_eng', 
                     'label' => 'sinopsis_eng', 
                     'rules' => 'max_length[10000]'),
				array('field' => 'servers[]', 
                     'label' => 'servers[]', 
                     'rules' => 'max_length[100]'),
				array('field' => 'sources[]', 
                     'label' => 'sources[]', 
                     'rules' => 'max_length[150]'),
				array('field' => 'online[]', 
                     'label' => 'online[]', 
                     'rules' => 'max_length[1]|is_natural')
    	));
		$this->status = $validate->run();
		
		if(isset($this->params['screenshots']) && !empty($this->params['screenshots'])){
			if(Sys::urlValid($this->params['screenshots']) == FALSE){
				$this->status = FALSE;
				$validate->_error_array['screenshots'] = lang('txt_invalid_url', 'screenshots');
			}
		}
		
		if(isset($this->params["servers"]) && isset($this->params['sources']) && isset($this->params["online"])){
			if(count($this->params["servers"]) != count($this->params["sources"]) 
				|| count($this->params["servers"]) != count($this->params["online"])){
				$this->status = FALSE;
				$validate->_error_array['values'] = lang('txt_missing_params','servers[]','sources[]', 'online[]');
			}
			
			if($this->status)
				foreach ($this->params["sources"] as $key => $itm)
					if(!sys::urlValid($itm)){
						$this->status = FALSE;
						$validate->_error_array['values'] = lang('txt_invalid_url', 'sources => "'.$itm.'"');
					}
		}
		
    	return $validate->_error_array;
    }
	
	 private function validate_get_episode(){
    	$validate = new MY_Form_validation();
    	$validate->setArrayValidate($this->params);
    	
    	$validate->set_rules(array(
    			array('field' => 'episode', 
                     'label' => 'episode', 
                     'rules' => 'required|is_natural_no_zero')
    	));
		$this->status = $validate->run();
		return $validate->_error_array;
    }
	
	private function validate_update_episode(){
    	$validate = new MY_Form_validation();
    	$validate->setArrayValidate($this->params);
    	
    	$validate->set_rules(array(
    			array('field' => 'season', 
                     'label' => 'season', 
                     'rules' => 'required|is_natural_no_zero'),
                 array('field' => 'episode', 
	                 'label' => 'episode', 
	                 'rules' => 'required|is_natural_no_zero'),
    			
    			array('field' => 'no_episode', 
                     'label' => 'no_episode', 
                     'rules' => 'is_natural'),
				array('field' => 'title', 
                     'label' => 'title', 
                     'rules' => 'max_length[100]'),
				array('field' => 'format_episode', 
                     'label' => 'format_episode', 
                     'rules' => 'max_length[10]'),
				array('field' => 'quality', 
                     'label' => 'quality', 
                     'rules' => 'max_length[20]'),
				array('field' => 'duration', 
                     'label' => 'duration', 
                     'rules' => 'max_length[30]'),
				array('field' => 'size', 
                     'label' => 'size', 
                     'rules' => 'max_length[20]'),
				array('field' => 'languaje', 
                     'label' => 'languaje', 
                     'rules' => 'max_length[20]'),
				array('field' => 'subtitles', 
                     'label' => 'subtitles', 
                     'rules' => 'max_length[20]'),
				array('field' => 'sinopsis_spa', 
                     'label' => 'sinopsis_spa', 
                     'rules' => 'max_length[10000]'),
				array('field' => 'sinopsis_eng', 
                     'label' => 'sinopsis_eng', 
                     'rules' => 'max_length[10000]'),
				array('field' => 'servers[]', 
                     'label' => 'servers[]', 
                     'rules' => 'max_length[100]'),
				array('field' => 'sources[]', 
                     'label' => 'sources[]', 
                     'rules' => 'max_length[150]'),
				array('field' => 'online[]', 
                     'label' => 'online[]', 
                     'rules' => 'max_length[1]|is_natural')
    	));
		$this->status = $validate->run();
		
		if(isset($this->params['screenshots']) && !empty($this->params['screenshots'])){
			if(Sys::urlValid($this->params['screenshots']) == FALSE){
				$this->status = FALSE;
				$validate->_error_array['screenshots'] = lang('txt_invalid_url', 'screenshots');
			}
		}
		
		if(isset($this->params["servers"]) && isset($this->params['sources']) && isset($this->params["online"])){
			if(count($this->params["servers"]) != count($this->params["sources"])
				|| count($this->params["servers"]) != count($this->params["online"])){
				$this->status = FALSE;
				$validate->_error_array['values'] = lang('txt_missing_params','servers[]','sources[]', 'online[]');
			}
			
			if($this->status)
				foreach ($this->params["sources"] as $key => $itm)
					if(!sys::urlValid($itm)){
						$this->status = FALSE;
						$validate->_error_array['values'] = lang('txt_invalid_url', 'sources => "'.$itm.'"');
					}
		}
		
    	return $validate->_error_array;
    }
	
	
	
	
	private function validate_tvdb_search(){
		$validate = new MY_Form_validation();
    	$validate->setArrayValidate($this->params);
    	
    	$validate->set_rules(array(
    			array('field' => 'title', 
                     'label' => 'title', 
                     'rules' => 'required|max_length[100]')
    	));
		$this->status = $validate->run();
		return $validate->_error_array;
	}
	
	private function validate_tvdb_serie(){
		$validate = new MY_Form_validation();
    	$validate->setArrayValidate($this->params);
    	
    	$validate->set_rules(array(
    			array('field' => 'tvdb_id', 
                     'label' => 'tvdb_id', 
                     'rules' => 'required|is_natural_no_zero')
    	));
		$this->status = $validate->run();
		return $validate->_error_array;
	}
	
	private function validate_tvdb_episode(){
		$validate = new MY_Form_validation();
    	$validate->setArrayValidate($this->params);
    	
    	$validate->set_rules(array(
    			array('field' => 'tvdb_id', 
                     'label' => 'tvdb_id', 
                     'rules' => 'required|is_natural_no_zero'),
				array('field' => 'season', 
                     'label' => 'season', 
                     'rules' => 'required|is_natural_no_zero'),
				array('field' => 'episode', 
                     'label' => 'episode', 
                     'rules' => 'required|is_natural_no_zero')
    	));
		$this->status = $validate->run();
		return $validate->_error_array;
	}
	
}